<template>
  <div class="index">
    <div class="header">
      <div class="left">
        <p class="text">作物：</p>
        <el-select v-model="value" placeholder="请选择" @change="changeCrop">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="right">
        <el-button size="medium" type="primary" icon="el-icon-arrow-left" @click="handleBack">返回</el-button>
      </div>
      <el-dialog title="新增措施" :visible.sync="dialogFormVisible">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="措施" prop="measure" required>
            <el-select v-model="ruleForm.measure" placeholder="请选择措施">
              <el-option
                v-for="item in ruleForm.options"
                :key="item.measure"
                :label="item.label"
                :value="item.measure"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="操作时间" required>
            <el-col :span="11">
              <el-form-item prop="date1">
                <el-date-picker v-model="ruleForm.date1" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期" style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="措施内容" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" :autosize="{minRows:5, maxRows:5}" />
          </el-form-item>
          <el-form-item label="添加图片">
            <el-upload
              ref="upload"
              action="1"
              :auto-upload="false"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-change="handleAvatarChange"
              :file-list="ruleForm.pictureUrls"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="determin=='true'" type="primary" @click="createNewMeasure">确 定</el-button>
          <el-button v-else type="primary" @click="putMeasure">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="contain">
      <h2 class="text-contain">实训措施管理</h2>
      <div class="block">
        <el-timeline :reverse="true">
          <el-timeline-item
            v-for="(activity, index) in activities"
            :key="index"
            placement="top"
            type="primary"
            :timestamp="activity.measureTime"
          >
            <el-card class="card">
              <h2>{{ activity.measureName }}</h2>
              <div class="text-picture">
                <div class="measure-contain">
                  {{ activity.measureContent }}
                </div>
                <div class="image-css">
                  <el-carousel trigger="click" height="200px">
                    <el-carousel-item v-for="item in activity.pictureUrls" :key="item.pictureUrl">
                      <el-image
                        style="width: 100%; height: 100%"
                        :fit="'scale-down'"
                        :src="item.pictureUrl"
                        @click.prevent="handlePictureCardPreview(index)"
                      />
                    </el-carousel-item>
                  </el-carousel>
                </div>
              </div>
              <div>
                <el-dialog class="preview-card" :visible.sync="dialogVisible">
                  <el-carousel class="preview-hover" trigger="click">
                    <el-carousel-item v-for="item in PreviewPictureUrls" :key="item" class="preview-picture">
                      <img width="100%" :src="item" alt="">
                    </el-carousel-item>
                  </el-carousel>
                </el-dialog>
              </div>
              <div class="button-contain">
                <el-button size="small" type="primary" @click="handleEdit(index)">编辑</el-button>
                <el-button size="small" type="danger" @click="handleDelete(index)">删除</el-button>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </div>
  </div>
</template>
<style>
.text{
    font-size: 14px;
}
</style>

<script>
import { postStudentManageDetail } from '@/api/manage'
import { getStudentManageDetail } from '@/api/manage'
import { deletStudentManageDetail } from '@/api/manage'
import { putStudentManageDetail } from '@/api/manage'

export default {
  data() {
    return {
      determin: 'true', // 编辑或者新建button的状态 v-if
      imageUrl: [],
      dialogVisible: false,
      PreviewPictureUrls: [],
      deleteImage: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      measureManageId: '',
      activities: [],
      ruleForm: {
        crop: '',
        date1: '',
        desc: '',
        pictureUrls: [],
        measure: '浇水',
        options: [
          {
            measure: '浇水',
            label: '浇水'
          },
          {
            measure: '施肥',
            label: '施肥'
          },
          {
            measure: '播种',
            label: '播种'
          },
          {
            measure: '除草',
            label: '除草'
          },
          {
            measure: '松土',
            label: '松土'
          }]
      },
      rules: {
        measure: [
          { required: true, message: '请选择操作', trigger: 'change' }
        ],
        date1: [
          { type: 'string', required: true, message: '请选择日期', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请填写措施内容', trigger: 'blur' }
        ]
      },
      options: [
        {
          value: '水稻',
          label: '水稻'
        },
        {
          value: '小麦',
          label: '小麦'
        },
        {
          value: '玉米',
          label: '玉米'
        },
        {
          value: '棉花',
          label: '棉花'
        },
        {
          value: '油菜',
          label: '油菜'
        }],
      value: '玉米',
      input: ''
    }
  },

  created() {
    this.newFetch()
  },

  methods: {
    newFetch() {
      const data = {
        studentId: this.$route.params.studentId,
        crop: this.value,
        studentId: 'strfing'
      }
      getStudentManageDetail(data).then(res => {
        this.activities = res.data
      })
    },
    handlePictureCardPreview(index) {
      this.PreviewPictureUrls = []
      for (let i = 0; i < this.activities[index].pictureUrls.length; i++) {
        this.PreviewPictureUrls.push(this.activities[index].pictureUrls[i].pictureUrl)
      }
      setTimeout(() => {
        this.dialogVisible = true
      }, 50)
    },
    changeCrop() {
      this.newFetch()
    },
    handleBack() {
      this.$router.go(-1)
    },
    resetForm(formName) {
      this.determin = 'true'
      this.ruleForm.desc = ''
      this.ruleForm.pictureUrls = []
      setTimeout(() => {
        this.dialogFormVisible = true
      }, 200)
    },
    handleRemove(file, fileList) {
      console.log(file)
      if (this.determin == 'false') {
        this.deleteImage.push(file.pictureId)
        console.log(this.deleteImage)
      }
    },
    handlePreview(file) {
      console.log(file)
    },
    handleAvatarChange(file, fileList) {
      this.ruleForm.pictureUrls.push(file)
      console.log(this.ruleForm.pictureUrls)
      console.log(fileList)
    },
    handleDelete(index) {
      console.log(this.activities[index].measureManageId)
      this.$confirm('确定要删除吗?删除后不能恢复', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletStudentManageDetail(this.activities[index].measureManageId).then(res => {
          if (res.data === true) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            this.$message({
              type: 'info',
              message: '删除失败!'
            })
          }
          this.newFetch()
        })
      })
    },
    changeValue() {
      console.log(this.ruleForm.value)
    },
    createNewMeasure() {
      console.log('this is create')
      this.dialogFormVisible = false
      console.log(this.ruleForm.date1)
      console.log(this.ruleForm.measure)
      const createMeasureData = new window.FormData()
      createMeasureData.append('crop', '玉米')
      createMeasureData.append('createTime', this.ruleForm.date1)
      createMeasureData.append('measureContent', this.ruleForm.desc)
      createMeasureData.append('studentId', 'student')
      createMeasureData.append('measure', this.ruleForm.measure)
      for (let i = 0; i < this.ruleForm.pictureUrls.length; i++) {
        createMeasureData.append('files', this.ruleForm.pictureUrls[i].raw)
      }
      postStudentManageDetail(createMeasureData).then(res => {
        console.log(res)
        this.newFetch()
      })
    },
    handleEdit(index) {
      this.measureManageId = this.activities[index].measureManageId
      console.log(this.measureManageId)
      this.determin = 'false'
      this.ruleForm.measure = this.activities[index].measureName
      this.ruleForm.desc = this.activities[index].measureContent
      this.ruleForm.date1 = this.activities[index].measureTime
      this.ruleForm.pictureUrls = []
      for (let i = 0; i < this.activities[index].pictureUrls.length; i++) {
        this.ruleForm.pictureUrls.push({ 'url': this.activities[index].pictureUrls[i].pictureUrl,
          'pictureId': this.activities[index].pictureUrls[i].pictureId })
      }
      setTimeout(() => {
        this.dialogFormVisible = true
      }, 200)
    },
    putMeasure() {
      this.dialogFormVisible = false
      const editForm = new FormData()
      editForm.append('crop', '玉米')
      editForm.append('createTime', this.ruleForm.date1)
      editForm.append('measureContent', this.ruleForm.desc)
      editForm.append('measureManageId', this.measureManageId)
      editForm.append('measure', this.ruleForm.measure)
      if (this.ruleForm.pictureUrls.length == 0) {
        editForm.append('files', this.ruleForm.pictureUrls)
      } else {
        for (let i = 0; i < this.ruleForm.pictureUrls.length; i++) {
          editForm.append('files', this.ruleForm.pictureUrls[i].raw)
        }
      }
      editForm.append('ids', this.deleteImage)
      putStudentManageDetail(editForm).then(res => {
        console.log(res)
        this.measureManageId = ''
        this.deleteImage = []
        this.newFetch()
      })
    }

  }
}
</script>
<style scoped>
.header{
    display: flex;
    justify-content: space-between;
  }
  .left{
    display: flex;
    justify-content: flex-start;
    font-size: 14px;
  }
  .contain{
      margin: 30px;
  }
  .measure-contain{
    margin: 5px;
    width: 50%;
  }
  .dialog-footer{
      text-align: center;
  }
  .text-contain{
    text-align: center;
    color: green;
  }
  .block{
    width: 80%;
    margin-left: 25%;
  }
  .card{
    width: 60%;
  }
  .button-contain{
    text-align: center;
    margin: 5px;
  }
  .avatar {
    width: 95px;
    height: 95px;
  }
  .image-css{
    width: 50%;
    height: 200px;
  }
  .text-picture{
    display: flex;
  }
  .preview-picture{
    width: 100%;
    height: 600px;
  }
  .preview-card{
    width: 100%;
    height: 800px;
  }
  .preview-hover{
    width: 100%;
    height: 500px;
  }
</style>
